/**
 * 
 */
package in.ac.nitw.imageProcessor;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

/**
 * Reduces any image to black and white
 * 
 * @author kislay
 * 
 */
public class BlackAndWhiteTransformer implements ImageTransformer {

    /*
     * (non-Javadoc)
     * 
     * @see
     * in.ac.nitw.imageProcessor.ImageTransformer#applyTransformation(java.awt
     * .image.BufferedImage)
     */
    @Override
    public BufferedImage applyTransformation(BufferedImage image) {
	BufferedImage blackWhite = new BufferedImage(image.getWidth(),
		image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
	WritableRaster raster = blackWhite.getRaster();

	for (int x = 0; x < raster.getWidth(); x++) {
	    for (int y = 0; y < raster.getHeight(); y++) {
		int argb = image.getRGB(x, y);
		int r = (argb >> 16) & 0xff;
		int g = (argb >> 8) & 0xff;
		int b = (argb) & 0xff;

		int l = (int) (.299 * r + .587 * g + .114 * b);
		raster.setSample(x, y, 0, l);
	    }
	}
	return blackWhite;
    }

}
